package fc;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import fc.entity.OaEntity;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.upgradetool.httpclient.org.apache.http.client.HttpClient;
import weaver.upgradetool.httpclient.org.apache.http.client.entity.UrlEncodedFormEntity;
import weaver.upgradetool.httpclient.org.apache.http.client.methods.HttpPost;
import weaver.upgradetool.httpclient.org.apache.http.impl.client.DefaultHttpClient;
import weaver.upgradetool.httpcore.org.apache.http.HttpResponse;
import weaver.upgradetool.httpcore.org.apache.http.NameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.message.BasicNameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.util.EntityUtils;
import weaver.upgradetool.wscheck.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DEL_REQUESTS implements Action{

	@Override
	public String execute(RequestInfo info) {
		String API_NAME="FC_DEL_REQUESTS";

		BaseBean log = new BaseBean();

		log.writeLog("接口"+API_NAME+"开始调用-----------------------------------");

		OaEntity oe=new OaEntity();

		String requestid = info.getRequestid();
		String tablename = info.getRequestManager().getBillTableName();
		oe.setREQUESTID(requestid);

		String sql=


"select  delrequestids FROM   "+tablename+"  WHERE   requestid="+requestid;
		log.writeLog("sql:"+""+sql);

		List<OaEntity> dts=new ArrayList<OaEntity>();

		RecordSet rs1 = new RecordSet();
		rs1.executeSql(sql);
		while(rs1.next()){
 			OaEntity dt1=new OaEntity();
			dt1.setJUDGE1(Util.null2String(rs1.getString("delrequestids")));
			dts.add(dt1);
		}
		String del_requestids="";
		for(int i=0;i<dts.size();i++){
			if("".equals(del_requestids)){}else{
				del_requestids+=",";
			}
			del_requestids+=dts.get(i).getJUDGE1();
		}
		String dels[]=del_requestids.split(",");
		int result=0;
		GetToken tok=new GetToken();
		String token=(String) tok.testGetoken().get("token");

		if(del_requestids.length()>0){

			String del_requestid="";

			String spk = FcHttpCon.SPK;
			RSA rsa = new RSA(null,spk);
			//对用户信息进行加密传输,暂仅支持传输 OA 用户 ID 放一个专门的用户吧
			String encryptUserid = rsa.encryptBase64(FcHttpCon.DEL_USERID,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
			for(int j=0;j<dels.length;j++){
				del_requestid=dels[j];
				String param="requestId="+del_requestid+"&otherParams={\"ismonitor\":\"1\"}";

				//调用 ECOLOGY 系统接口
				String data = HttpRequest.post(FcHttpCon.OAURL+"/api/workflow/paService/deleteRequest")
						.header("appid",FcHttpCon.APPID)
						.header("token",token)
						.header("userid",encryptUserid)
						.body(param)
						.execute().body();

				try {
					Object succesResponse = JSON.parse(data);
					Map map = (Map)succesResponse;
 					log.writeLog("requestid:"+del_requestid+",结果:"+data);
					if("SUCCESS".equals(map.get("code"))){
						result+=1;
					}
				}catch (Exception e){

				}


			}

		}

		//这里判断一下是不是都删除了
		if(dels.length-result>0){
			info.getRequestManager().setMessagecontent("有"+(dels.length-result)+"条删除！");
		}
		return SUCCESS;


	}



} 